package cn.edu.xmu.orderpayment.payment.Controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.orderpayment.payment.Service.ErrorAccountService;
import cn.edu.xmu.orderpayment.payment.model.vo.ModificationBody;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Api(value = "支付", tags = "payment")
@RestController
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public class ErrorAccountController {

    @Autowired
    private ErrorAccountService paymentService;

    /**
     * 平台管理员查询错账信息
     * @param loginUser
     * @param loginUsername
     * @param shopId
     * @param documentId
     * @param state
     * @param beginTime
     * @param endTime
     * @param page
     * @param pageSize
     * @return
     * @Auther Zhang Liu
     */
    @Audit(departName = "shops")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization ", value = "token", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "shopId", value = "店铺id", required = true, dataType = "Integer", paramType = "path"),
    })
    @GetMapping("/shops/{shopId}/erroraccounts")
    public Object adminSelecterrorAccounts(@LoginUser Long loginUser, @LoginName String loginUsername,
                                           @PathVariable("shopId")Long shopId,
                                           @RequestParam(value = "documentId",required = false) String documentId,
                                           @RequestParam(value = "state",required = false) Byte state,
                                           @RequestParam(value = "beginTime",required = false) @DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") LocalDateTime beginTime,
                                           @RequestParam(value = "endTime",required = false) @DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") LocalDateTime endTime,
                                           @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                                           @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize){
        if(!shopId.equals(0L))
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        ReturnObject returnObject = paymentService.adminSelecterrorAccounts(null,documentId,state,beginTime,endTime,page,pageSize);
        return Common.decorateReturnObject(returnObject);
    }

    /**
     * 平台管理员查询错账信息详情
     * @param loginUser
     * @param loginUsername
     * @param shopId
     * @param id
     * @return
     * @Author Zhang Liu
     */
    @Audit(departName = "shops")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization ", value = "token", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "shopId", value = "店铺id", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "id", value = "errorAccountid", required = true, dataType = "Integer", paramType = "path")
    })
    @GetMapping("/shops/{shopId}/erroraccounts/{id}")
    public Object adminSelectCertainerrorAccount(@LoginUser Long loginUser, @LoginName String loginUsername,
                                                 @PathVariable("shopId")Long shopId,
                                                 @PathVariable("id") Long id)
    {
        if(!shopId.equals(0L))
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        ReturnObject returnObject=paymentService.adminSelectCertainerrorAccounts(id);
        return Common.decorateReturnObject(returnObject);
    }

    /**
     * 平台管理员修改错账信息详情
     * @param shopId
     * @param id
     * @param modificationBody
     * @param bindingResult
     * @param loginUserId
     * @param loginUserName
     * @return
     * @Auther Zhang Liu
     */
    @Audit(departName = "shops")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization ", value = "token", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "shopId", value = "店铺id", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "id", value = "errorAccountid", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name="changeBody",value="更新信息",dataType = "ChangeBody",paramType = "body"),

    })
    @PutMapping("/shops/{shopId}/erroraccounts/{id}")
    public Object adminUpdateCertainerrorAccount(@PathVariable("shopId")Long shopId,
                                                 @PathVariable("id") Long id, @RequestBody ModificationBody modificationBody, BindingResult bindingResult,
                                                 @LoginUser Long loginUserId, @LoginName String loginUserName)
    {
        if(bindingResult.hasErrors()){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.FIELD_NOTVALID,"传入的RequestBody参数格式不合法"));
        }
        if(!shopId.equals(0L))
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        ReturnObject returnObject=paymentService.adminUpdateCertainErrorAccounts(id,modificationBody,loginUserId,loginUserName);
        return Common.decorateReturnObject(returnObject);
    }
}
